package com.xiyounet.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiyounet.login.entity.IUser;
import com.xiyounet.login.mapper.UserMapper;
import com.xiyounet.login.service.LoginService;
import com.xiyounet.login.service.UserService;
import com.xiyounet.login.vo.Result;
import com.xiyounet.login.vo.StatusEnum;
import com.xiyounet.login.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginService loginService;

    /**
     * 通过手机号和密码找User
     * @param phone
     * @param password
     * @return
     */
    @Override
    public IUser findUser(String phone, String password) {
        LambdaQueryWrapper<IUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IUser::getPhone,phone);
        queryWrapper.eq(IUser::getPassword,password);
        queryWrapper.select(IUser::getUserId,IUser::getUserName,IUser::getEmail,IUser::getPhone,IUser::getSex,IUser::getStatus,IUser::getBalance);
        queryWrapper.last("limit 1");

        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 存储用户信息
     * @param user
     */
    @Override
    public void saveUser(IUser user) {

        userMapper.insert(user);
    }

    /**
     * 通过手机号找用户
     * @param phone
     * @return
     */
    @Override
    public IUser getUserByPhone(String phone) {
        LambdaQueryWrapper<IUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IUser::getPhone,phone);
        queryWrapper.select(IUser::getUserId,IUser::getUserName);
        queryWrapper.last("limit 1");

        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 通过用户名找用户
     * @param username
     * @return
     */
    @Override
    public IUser getUserByUserName(String username) {
        LambdaQueryWrapper<IUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IUser::getUserName,username);
        queryWrapper.select(IUser::getUserId,IUser::getPhone);
        queryWrapper.last("limit 1");

        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 通过token查找当前用户信息
     * @param token
     * @return
     */
    @Override
    public Result getUserByToken(String token) {

        IUser user = loginService.checkToken(token);
        if(user==null){
            return Result.fail(10003,"token不存在！");
        }
        System.out.println(user);
        //封装User对象
        UserVo userVo = new UserVo();
        userVo.setUserName(user.getUserName());
        userVo.setEmail(user.getEmail());
        userVo.setBalance(user.getBalance());
        if(user.getSex()==1){
            userVo.setSex("男");
        }else {
            userVo.setSex("女");
        }
        //设置用户状态
        Integer status = user.getStatus();
        if(status==0){
            userVo.setStatus("不在线");
        }else if(status==1){
            userVo.setStatus("在线");
        }else if(status==2){
            userVo.setStatus("隐身");
        }else if(status==3){
            userVo.setStatus(StatusEnum.FELLING_WELL.getDes());
        }else if(status==4){
            userVo.setStatus(StatusEnum.FELLING_BAD.getDes());
        }

        return Result.success(userVo);
    }

    /***
     * 改变用户状态
     * @param sta
     * @return
     */
    @Override
    public Result changeStatus(Integer sta) {

        return null;
    }
}
